package vip.yyzhu.redisadmin.api.key.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import vip.yyzhu.redisadmin.jopo.ResData;
import vip.yyzhu.redisadmin.jopo.ResJson;
import vip.yyzhu.redisadmin.aop.verifyKeyType.VerifyKeyType;
import vip.yyzhu.redisadmin.redis.RedisList;
import vip.yyzhu.redisadmin.util.Tool;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/key/str", produces = "application/json;charset=UTF-8")
@SuppressWarnings("all")
public class StrController {
    @Autowired
    RedisList redisList;
    @Autowired
    Tool tool;


    @VerifyKeyType("string")
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    public Object get(HttpServletRequest request,
                      @RequestParam String key) throws Exception{
        val jedis = tool.getJedis(request);
        String set = jedis.get(key);
        return new ResJson(200, "成功", set);
    }

    @VerifyKeyType(value = "string",empty = true)
    @RequestMapping(value = "/set", method = RequestMethod.POST)
    public Object set(HttpServletRequest request,
                         @RequestParam String key,
                         @RequestParam String con) throws Exception{
        val jedis = tool.getJedis(request);
        String set = jedis.set(key, con);
        Map map = new HashMap();
        map.put("key", key);
        map.put("value", con);
        ResData resData = new ResData(map);
        resData.setRedis(set);
        return new ResJson(200, "成功", resData);
    }
}
